﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MT.CustomWindow;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WpfMvvmDemo.Models;

namespace WpfMvvmDemo.ViewModels
{
    /// <summary>
    /// 测试ViewModel
    /// </summary>
    public class TestViewModel:ObservableObject
    {
        #region 文本常用绑定

        /// <summary>
        /// 字符串绑定
        /// </summary>
        private string textStr = "字符串";
        public string TextStr
        {
            get { return textStr; }
            set { SetProperty(ref textStr, value); }
        }

        /// <summary>
        /// 文字绑定
        /// </summary>
        private int textNum = 8848;
        public int TextNum
        {
            get { return textNum; }
            set { SetProperty(ref textNum, value); }
        }

        /// <summary>
        /// 日期绑定
        /// </summary>
        private DateTime curDateTime = DateTime.Now;
        public DateTime CurDateTime
        {
            get { return curDateTime; }
            set { SetProperty(ref curDateTime, value); }
        }

        #endregion

        #region 按钮常用绑定

        /// <summary>
        /// 按钮1文本
        /// </summary>
        private string btnContext_1 = "按钮文字和命令绑定";
        public string BtnContext_1
        {
            get { return btnContext_1; }
            set { SetProperty(ref btnContext_1, value); }
        }

        /// <summary>
        /// 按钮显示和隐藏
        /// </summary>
        private Visibility btnVisibility = Visibility.Visible;
        public Visibility BtnVisibility
        {
            get { return btnVisibility; }
            set { SetProperty(ref btnVisibility, value); }
        }

        /// <summary>
        /// 按钮1命令
        /// </summary>
        private RelayCommand btnCommand_1;
        public RelayCommand BtnCommand_1
        {
            get
            {
                if (btnCommand_1 == null)
                {
                    btnCommand_1 = new RelayCommand(BtnCommand_1_Execute);
                }

                return btnCommand_1;
            }
        }

        /// <summary>
        /// 按钮命令
        /// </summary>
        private RelayCommand<string> radioBtnCommand;
        public RelayCommand<string> RadioBtnCommand
        {
            get
            {
                if (radioBtnCommand == null)
                {
                    radioBtnCommand = new RelayCommand<string>((tempParameter) =>
                    {
                        BtnVisibility = tempParameter == "显示" ? Visibility.Visible : Visibility.Hidden;
                    });
                }
                return radioBtnCommand;
            }
        }

        /// <summary>
        /// 按钮是否使能
        /// </summary>
        private bool isEnable = true;
        public bool IsEnable
        {
            get { return isEnable; }
            set { SetProperty(ref isEnable, value); }
        }

        /// <summary>
        /// 切换按钮使能状态
        /// </summary>
        private RelayCommand<string> changeBtnEnableStateCommand;
        public RelayCommand<string> ChangeBtnEnableStateCommand
        {
            get
            {
                if (changeBtnEnableStateCommand == null)
                {
                    changeBtnEnableStateCommand = new RelayCommand<string>((tempParameter) =>
                    {
                        IsEnable = tempParameter == "使能" ? true : false;
                    });
                }
                return changeBtnEnableStateCommand;
            }
        }

        #endregion


        #region Combox 常用绑定

        /// <summary>
        /// 选中索引 注意 Combox的SelectedIndex和SelectedValue 最好不要同时绑定
        /// </summary>
        private int comboxSelectedIndex = 1;
        public int ComboxSelectedIndex
        {   
            get { return comboxSelectedIndex; }
            set { SetProperty(ref comboxSelectedIndex, value); }
        }

        /// <summary>
        /// 选中项
        /// </summary>
        private string comboxSelectedValue = "第 1 项";
        public string ComboxSelectedValue
        {
            get { return comboxSelectedValue; }
            set { SetProperty(ref comboxSelectedValue, value); }
        }

        /// <summary>
        /// Combox数据源
        /// </summary>
        private ObservableCollection<string> comboxItemSource = new ObservableCollection<string>();
        public ObservableCollection<string> ComboxItemSource
        {
            get { return comboxItemSource; }
            set { SetProperty(ref comboxItemSource, value); }
        }

        #endregion

        #region CheckBox 常用绑定

        private bool isChecked = true;
        public bool IsChecked
        {
            get { return isChecked; }
            set { SetProperty(ref isChecked, value); }
        }

        #endregion

        #region DataGrid 常用绑定

        /// <summary>
        /// 当前选中项
        /// </summary>
        private TestModel selectedTestModel;
        public TestModel SelectedTestModel
        {
            get { return selectedTestModel; }
            set { SetProperty(ref selectedTestModel, value); }
        }

        /// <summary>
        /// 具有通知的集合属性
        /// </summary>
        private ObservableCollection<TestModel> allTestModels = new ObservableCollection<TestModel>();
        public ObservableCollection<TestModel> AllTestModels
        {
            get { return allTestModels; }
            set { SetProperty(ref allTestModels, value); }
        }

        /// <summary>
        /// DataGrid的PreviewMouseLeftButtonUp事件处理
        /// </summary>
        private RelayCommand dataGridPreviewMouseLeftButtonUpCommand;
        public RelayCommand DataGridPreviewMouseLeftButtonUpCommand
        {
            get
            {
                if (dataGridPreviewMouseLeftButtonUpCommand == null)
                {
                    dataGridPreviewMouseLeftButtonUpCommand = new RelayCommand(() =>
                    {
                        //MessageBoxWindow.ShowMessage($"当前选中项，姓名 {SelectedTestModel.Name}");
                    });
                }

                return dataGridPreviewMouseLeftButtonUpCommand;
            }
        }

        /// <summary>
        /// 查看报告
        /// </summary>
        private RelayCommand<TestModel> selectedItemCheckReportCommand;
        public RelayCommand<TestModel> SelectedItemCheckReportCommand
        {
            get
            {
                if (selectedItemCheckReportCommand == null)
                {
                    selectedItemCheckReportCommand = new RelayCommand<TestModel>((tempParameter) =>
                    {
                        MessageBoxWindow.ShowMessage($"查看报告，姓名 {tempParameter.Name}");
                    });
                }

                return selectedItemCheckReportCommand;
            }
        }

        #endregion


        #region ListBox 常用绑定

        /// <summary>
        /// 当前选中索引
        /// </summary>
        private int listSelectedIndex = -1;
        public int ListSelectedIndex
        {
            get => listSelectedIndex;
            set => SetProperty(ref listSelectedIndex, value);
        }

        /// <summary>
        /// 当前选中的ImageViewModel
        /// </summary>
        private ImageViewModel selectedImageViewModel;
        public ImageViewModel SelectedImageViewModel
        {
            get => selectedImageViewModel;
            set => SetProperty(ref selectedImageViewModel, value);
        }

        /// <summary>
        /// 所有图像集合
        /// </summary>
        private ObservableCollection<ImageViewModel> allImageViewModels = new ObservableCollection<ImageViewModel>();
        public ObservableCollection<ImageViewModel> AllImageViewModels
        {
            get => allImageViewModels;
            set => SetProperty(ref allImageViewModels, value);
        }

        /// <summary>
        /// ListBox的SelectionChanged事件处理
        /// </summary>
        private RelayCommand listBoxSelctionChangedCommand;
        public RelayCommand ListBoxSelctionChangedCommand
        {
            get
            {
                if (listBoxSelctionChangedCommand == null)
                {
                    listBoxSelctionChangedCommand = new RelayCommand(() =>
                    {
                        foreach (var item in AllImageViewModels)
                        {
                            item.BorderBrush = Brushes.White;
                        }

                        SelectedImageViewModel.BorderBrush = Brushes.Red;
                    });
                }

                return listBoxSelctionChangedCommand;
            }
        }

        #endregion

        public TestViewModel()
        {
            for (int i = 0; i < 5; i++)
            {
                ComboxItemSource.Add($"第 {i} 项");

                //DataGrid测试数据
                TestModel testModel = new TestModel { Name = $"姓名{i}", Age = i * 10,  Gender = i % 2 == 0 ? "男" : "女", };
                testModel.AnalysisReportAction = AnalysisReportActionFunc;
                AllTestModels.Add(testModel);

                //ListBox测试数据
                string imagePath = AppDomain.CurrentDomain.BaseDirectory + $"\\Images\\image{i}.jpg";

                ImageViewModel imageViewModel = new ImageViewModel();
                imageViewModel.ImageTitle = $"图片{i}";
                imageViewModel.CurImageSource = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
                AllImageViewModels.Add(imageViewModel);
            }
        }

        private void BtnCommand_1_Execute()
        {
            MessageBoxWindow.ShowMessage("按钮被点击了");
        }

        private void AnalysisReportActionFunc(TestModel testModel)
        {
            MessageBoxWindow.ShowMessage($"分析报告，姓名 {testModel.Name}");
        }
    }
}
